# -*- coding: UTF-8 -*-
from django.conf import settings
from django.conf.urls.defaults import include, patterns, handler404, handler500, url
from blogmaker.blog.models import Entry
from django.contrib.syndication.views import feed
from django.views.generic.date_based import object_detail, archive_month, archive_year, archive_index
from django.http import HttpResponseRedirect
from blogmaker.blog.views import view_tagged_items, view_tags, view_user_items, search, extras_day, trackback
from blogmaker.blog.feeds import blogmakerEntryFeed,blogmakerTagFeed
from blogmaker.blog.sitemaps import sitemaps
from blogmaker.blog.trackback_views import postTrackbacks
from blogmaker.comments.feeds import LatestCommentsFeed
from datetime import date
from django.contrib import admin

admin.autodiscover()

feeds = {
    'posts' : blogmakerEntryFeed,
    'comments': LatestCommentsFeed,
    'tags': blogmakerTagFeed,
}

def wp_redirect_posts(request):
    "To mantain WP feeds post url compatibility"
    return HttpResponseRedirect("/feeds/posts/")

def wp_redirect_comments(request):
    "To mantain WP feeds comments url compatibility"
    return HttpResponseRedirect("/feeds/comments/")

info_dict = {
    'queryset': Entry.objects.filter(active=True),
    'date_field': 'pub_date',

}

urlpatterns = patterns('',
    (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
    (r'^tag/(?P<tag>[a-zA-Z0-9_.-]+)/$', view_tagged_items),
    (r'^tag/$', view_tags),
    (r'^user/(?P<user>[a-zA-Z0-9_.-]+)/$', view_user_items),
    (r'^search/$', search),
    (r'^feeds/(?P<url>.*)/$', feed, {'feed_dict': feeds}),
    (r'^feed/$', wp_redirect_posts),
    (r'^comments/', include('blogmaker.comments.urls')),
    (r'^comments/feed/$', wp_redirect_comments),
    (r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\w{1,2})/$', extras_day, dict(info_dict, month_format='%m')),
    (r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$', object_detail, dict(info_dict, slug_field='slug', month_format='%m', allow_future=False)),
    (r'^\d{4}/\d{2}/\d{1,2}/(?P<slug>[-\w]+)/trackback/$', trackback),
    (r'^\d{4}/\d{2}/\d{1,2}/(?P<slug>[-\w]+)/postTrackbacks/$', postTrackbacks),
    (r'^(?P<year>\d{4})/(?P<month>\d{2})/$', archive_month, dict(info_dict, month_format='%m')),
    (r'^(?P<year>\d{4})/$', archive_year, dict(info_dict, make_object_list=True)),
    (r'^$', archive_index, dict(info_dict, num_latest=5, date_field='pub_date',
                                extra_context={'today':date.today()})),
    (r'^blogmaker/', include('blogmaker.blog.admin_urls')),
    (r'^table/$', 'blogmaker.blog.views.table'),
    (r'^comments/',include('blogmaker.comments.urls')),
    (r'^planet/', include('feedjack.urls')),
    # Photologue
    (r'^photologue/',include('photologue.urls')),
    #admin
    (r'^admin/', include(admin.site.urls)),
    url(r'^admin_tools/', include('admin_tools.urls')),
    # Robots
    (r'^robots.txt$', include('robots.urls')),
    #Ajax
    (r'^ajax/',include('blogmaker.blog.ajax_urls')),
)


# We're going to use the Django server in development, so we'll server
# also the estatic content.
if settings.DEBUG:
	urlpatterns += patterns('',
       (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root':'./media/'}),
    )


